<?php
/**
 * Created by PhpStorm.
 * User: huangwy
 * Date: 2018/1/8
 * Time: 11:45
 */

namespace Admin\Controller;
use Admin\Controller\BaseController;

use Com\Wechat;
use Com\WechatAuth;

class WxManageController extends BaseController{
    /**
     * 微信信息设置
     */
    public function setting(){
        //获取微信设置信息
//        $wxSetting = M('setting')->select();//
        $prefix = C('DB_PREFIX');
        $wxSetting = M('setting')->query("select * from {$prefix}wechat");
        $this->assign('wxSetting',$wxSetting[0]);
        $this->display();
    }

    /**
     * 更新微信信息
     */
    public function updateWxchat(){
        $data = I('post.');
        D('Wechat')->update($data);
        $this->ajaxReturn('修改微信配置成功。');
    }

    /**
     * 微信栏目
     */
    public function index(){
        $option = M('wxmenu')->select();
        $option = getMenu($option,'id','parentid');
        $this->assign('list',$option);
        $this->display();
    }

    public function add(){
        //查询一级栏目
        $option = M('wxmenu')->where('parentid=0 and isview=1')->select();
//        $option = getMenu($option,'id','parentid');
        $this->assign('wxoption',$option);
        $this->display('form');
    }
    public function edit(){
        $id = I('get.id');
        //查询一级栏目
        $option = M('wxmenu')->where('parentid=0 and isview=1')->select();
        //查询对应信息
        $wxmenu = M('wxmenu')->where('id='.$id)->find();
        $this->assign('wxoption',$option);
        $this->assign('wxmenu',$wxmenu);
        $this->display('form');
    }
    public function del()
    {
        $ids = I('get.ids');
        $del = D('Wechat')->wxMenuDel($ids);
        if ($del) {
            $del = is_string($del)?$del:'恭喜，菜单删除成功！';
            $this->ajaxReturn($del);
        } else {
            $this->ajaxReturn('参数错误！');
        }
    }

    public function update(){
        $info = I('post.');
        $update = D('Wechat')->wxMenuUpdate($info);
        if ($update) {
            $update = is_string($update)?$update:'操作成功！';
            $this->ajaxReturn($update);
        }else{
            $this->ajaxReturn('操作失败！');
        }
    }

    public function setMenu(){
        $Model = D('wxmenu');

        $where['isview'] = 1;
        $list = $Model->where($where)->select();

        $memu = $this->memuJson($list);

        $appid     = 'wx6a5caa24d776fc99';
        $appsecret = 'afbb533613e8fb0a7490649c3bb47e48';

        $token = session("token");

        if($token){
            $auth = new WechatAuth($appid, $appsecret, $token);
        } else {
            $auth  = new WechatAuth($appid, $appsecret);
            $token = $auth->getAccessToken();

            session(array('expire' => $token['expires_in']));
            session("token", $token['access_token']);
        }
        $auth->menuCreate($memu);
    }


    private function memuJson($data=''){
        if(!$data){
            return false;
        }
        foreach ($data as $rs) {
            $plist[$rs['parentid']][] = $rs;
        }
        $jsonlist = array();
        foreach ($plist[0] as $rs) {
            $memu = $plist[$rs['id']];
            //有二级栏目
            if($memu){
                $json['name'] = $rs['name'];
                $sub_button = array();
                foreach ($memu as $row) {
                    $sub_button[] = $this->memuOne($row);
                }
                $json['sub_button'] = $sub_button;
            }else{
                $json = $this->memuOne($memu);
            }
            $jsonlist[] = $json;
        }
        $button['button'] = $jsonlist;
//        dump($button);die;
        return json_encode($button);
    }
    /**
     * 返回单菜单结构
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    private function memuOne($data){
        //var_dump($data);die;
        $type = 'click';
        $key  = '';
        //内部栏目
        if($data['type']==1){
            $key = 'weixin_news_'.$data['id'];
        }
        //URL
        if($data['type']==2){
            $type = 'view';
        }
        //内部标识
        if($data['type']==1){
            $key = $data['key'];
        }

        $memu['name'] = $data['name'];
        $memu['type'] = $type;
        if($type=='view'){
            $memu['url'] = $data['url'];
        }else{
            $memu['key'] = $key;
        }
        return $memu;
    }
}
